package com.sise.controller.wechat;

import com.sise.bean.CodeMsg;
import com.sise.bean.ProductTypeBean;
import com.sise.entity.ProductInfo;
import com.sise.entity.ProductType;
import com.sise.service.IProductInfoService;
import com.sise.service.IProductTypeService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 */
@RestController
@RequestMapping("/sise/wechat/product-info")
public class WechatProductInfoController {

    @Resource
    private IProductInfoService productInfoService;

    @Resource
    private IProductTypeService productTypeService;

    @RequestMapping("/findUpAllByStoreId")
    public List<ProductInfo> findUpAllByStoreId(Integer storeId) {
        return productInfoService.findUpAll(storeId);
    }

    @RequestMapping("/findProductTypeByStoreId")
    public List<ProductTypeBean> findProductTypeByStoreId(Integer storeId) {
        List<ProductType> productTypeList = productTypeService.findByStoreId(storeId);
        List<ProductTypeBean> productTypeBeanList = new ArrayList<ProductTypeBean>();

        for (ProductType productType : productTypeList) {
            ProductTypeBean productTypeBean = new ProductTypeBean();
            productTypeBean.setProductTypeId(productType.getProductTypeId());
            productTypeBean.setProductTypeName(productType.getProductTypeName());

            List<ProductInfo> productInfoList = productInfoService.findUpProductByStoreIdAndProductTypeId(productType.getStoreId(), productType.getProductTypeId());
            productTypeBean.setProductInfoList(productInfoList);

            productTypeBeanList.add(productTypeBean);
        }
        return productTypeBeanList;
    }

    @RequestMapping("/searchProduct")
    public CodeMsg<List<ProductInfo>> searchProduct(String search, Integer current) {
        return productInfoService.searchProduct(search, current);
    }
}
